﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using Telerik.Web.UI;

namespace HCFramework.Product.FilterManage
{
    public partial class ChooseFilter : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void BindData(int propertyID)
        {
            litPropertyID.Text = propertyID.ToString();
            RadComboBox1.DataSource = C.ProductPropertyController.GetByID(propertyID).PropertyFilters;
            RadComboBox1.DataValueField = "PropertyFilterID";
            RadComboBox1.DataTextField = "Value";
            RadComboBox1.DataBind();
        }

        protected void RadComboBox1_ItemDataBound(object sender, Telerik.Web.UI.RadComboBoxItemEventArgs e)
        {
            CheckBox checkBox = (CheckBox)e.Item.FindControl("CheckBox1");
            checkBox.Text = e.Item.Text;
            checkBox.InputAttributes["value"] = e.Item.Value;
            checkBox.InputAttributes["text"] = e.Item.Text;
            checkBox.InputAttributes["propertyid"] = litPropertyID.Text.Replace("-", "");
        }

        public List<int> SelectedIDs
        {
            get
            {
                List<int> ret = new List<int>();
                foreach (RadComboBoxItem item in RadComboBox1.Items)
                {
                    CheckBox checkBox = (CheckBox)item.FindControl("CheckBox1");
                    if (checkBox.Checked) ret.Add(int.Parse(item.Value));
                }
                return ret;
            }
            set
            {
                foreach (RadComboBoxItem item in RadComboBox1.Items)
                {
                    CheckBox checkBox = (CheckBox)item.FindControl("CheckBox1");
                    if (value.Contains(int.Parse(item.Value))) checkBox.Checked = true;
                    else checkBox.Checked = false;
                }
            }
        }
    }
}